package com.yazi.mxz.common.db.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.StoreGoodsDao;
import com.yazi.mxz.common.entity.beans.Category;
import com.yazi.mxz.common.entity.beans.Goods;
import com.yazi.mxz.common.entity.beans.GoodsClassify;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicDao;

/**
 * 
 * @author mo_yq5
 * @date 2014-2-1
 */
@Repository("storeGoodsDao")
public class StoreGoodsDaoImpl extends BasicDao<StoreGoods> implements StoreGoodsDao {

	public StoreGoodsDaoImpl() {
		super(StoreGoods.class);
	}

	@Override
	public Criteria getExample(StoreGoods sg, String searchWord) {
		String keywords = null;
		if (null != sg) {
			keywords = sg.getKeywords();
			sg.setKeywords(null);
		}
		Criteria c = super.getExample(sg, searchWord);
		
		if (null != sg) {
			// 商品库
			Goods goods = sg.getGoods();
			if (null != goods) {
				// 商品库ID
				if (null != goods.getGoodsId()) {
					c.add(Restrictions.eq("goods.goodsId", goods.getGoodsId()));
				} else {
					// ...商品库其它条件
				}
			}
			// 店铺
			Store store = sg.getStore();
			if (null != store) {
				// 店铺ID
				if (null != store.getStoreId()) {
					c.add(Restrictions.eq("store.storeId", store.getStoreId()));
				} else {
					// ...店铺其它条件
				}
				
			}
			// 系统分类
			Category category = sg.getCategory();
			if (null != category) {
				// 分类ID
				if (null != category.getCatId()) {
					DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
					dc.add(Restrictions.sqlRestriction("1=1 START WITH cat_id=" + category.getCatId() + " CONNECT BY PRIOR cat_id= parent_id "));
					dc.setProjection(Projections.property("catId"));
					c.add(Subqueries.propertyIn("category.catId", dc));
				} else {
					// ...分类其它条件
				}
			}
			// （企业）自定义分类
			GoodsClassify classify = sg.getGoodsClassify();
			if (null != classify) {
				// 分类ID
				if (null != classify.getClassifyId()) {
					DetachedCriteria dc = DetachedCriteria.forClass(GoodsClassify.class);
					dc.add(Restrictions.sqlRestriction("1=1 START WITH classify_id=" + classify.getClassifyId() + " CONNECT BY PRIOR classify_id= parent_id "));
					dc.setProjection(Projections.property("classifyId"));
					c.add(Subqueries.propertyIn("goodsClassify.classifyId", dc));
				} else {
					// ...分类的其它条件
				}
				
			}
			//...其它关联对象条件过虑
		}
		
		// 商品关键字属性主要用于查询类似商品
		if(null != keywords && !keywords.isEmpty()){
			String[] keywordList = keywords.split(",");			
			List<LogicalExpression> leList = new ArrayList<LogicalExpression>();
			
			//遍历待查询的关键字，将其加入条件中
			for(String keyword : keywordList){
				if(null != keyword && !keyword.isEmpty()){
					String likeWord0 = keyword;
					String likeWord1 = keyword + ",%";
					String likeWord2 = "%," + keyword + ",%";
					String likeWord3 = "%," + keyword;
					LogicalExpression el = Restrictions.or(Restrictions.like("keywords", likeWord0),
						 	Restrictions.or(Restrictions.like("keywords", likeWord1),
						 			Restrictions.or(Restrictions.like("keywords", likeWord2),
						 					Restrictions.like("keywords", likeWord3))));
					leList.add(el);
				}
				
			}	
			LogicalExpression[] les = new LogicalExpression[leList.size()];
			for (int i = 0; i < leList.size(); i++) {
				les[i] = leList.get(i);
			}
			c.add(Restrictions.or(les));
		}
		
		if (null != searchWord && !searchWord.isEmpty()) {
			// 搜索关键字可查商品名称,商品编号
			String likeWord = "%"+ searchWord + "%";
			c.add(Restrictions.or(Restrictions.like("name", likeWord), 
						Restrictions.like("barCode", likeWord)));
		}
		
		return c;
	}

	@Override
	public Page<StoreGoods> listFromSameOrder(Long storeGoodsId, Searching si) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("storeGoodsId", storeGoodsId);
		return list("listFromSameOrder", map, si);
	}
	
	@Override
	public void false4AtDiscount() {
		executeUpdate("false4AtDiscountHql", null);
	}

	@Override
	public void true4AtDiscount() {
		executeUpdate("true4AtDiscountHql", null);
	}

	@Override
	public void false4AtDiscountLocal() {
		executeUpdate("false4AtDiscountLocalHql", null);
	}

	@Override
	public void true4AtDiscountLocal() {
		executeUpdate("true4AtDiscountLocalHql", null);
	}

	@Override
	public void false4AtGift() {
		executeUpdate("false4AtGiftHql", null);
	}

	@Override
	public void true4AtGift() {
		executeUpdate("true4AtGiftHql", null);
	}
	
	@Override
	public void false4AtGiftLocal() {
		executeUpdate("false4AtGiftLocalHql", null);
	}

	@Override
	public void true4AtGiftLocal() {
		executeUpdate("true4AtGiftLocalHql", null);
	}


	@Override
	public void updateMonthDealByTime(Long lastMonth, Long nowTime) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("lastMonth", lastMonth);
		param.put("nowTime", nowTime);
		executeUpdate("updateMonthDealByTime", param);
	}

	@Override
	public void updateWeekDealByTime(Long lastWeek, Long nowTime) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("lastWeek", lastWeek);
		param.put("nowTime", nowTime);
		executeUpdate("updateWeekDealByTime", param);
	}

	@Override
	public void updateAllDealByTime() {
		executeUpdate("updateAllDealByTime", null);
	}

	@Override
	public Page<StoreGoods> listByStockGtZero(StoreGoods sg, Searching si) {
		Criteria c = getExample(sg, si.getSearchWord());
		c.add(Restrictions.gt("stock", 0f));
		return list(c, si);
	}
	
	@Override
	public Page<StoreGoods> listByBarCodeLike(StoreGoods sg, Searching si) {
		String likeBarCode = "%" + sg.getBarCode() + "%";
		sg.setBarCode(null);
		Criteria c = getExample(sg, si.getSearchWord());
		c.add(Restrictions.like("barCode", likeBarCode));
		return list(c, si);
	}
	
	@Override
	public void move(long fromClassifyId, long toClassifyId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("from_id", fromClassifyId);
		param.put("to_id", toClassifyId);
		executeUpdate("updateClassifyIdSql", param);
	}

	@Override
	public Page<StoreGoods> listByGtUpdateTime(StoreGoods sg, Searching si) {
		long updateTime = sg.getUpdateTime();
		sg.setUpdateTime(null);
		Criteria c = getExample(sg, si.getSearchWord());
		c.add(Restrictions.gt("updateTime", updateTime));
		return list(c, si);
	}

	@Override
	public void moveByCatId(long fromCatId, long toCatId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("from_id", fromCatId);
		param.put("to_id", toCatId);
		executeUpdate("updateCatIdSql", param);
	}
}
